using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace Model
{
    public partial class Tbl002_Category : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlBoolean		_caStatus, _caType;
			private SqlString		_caLink, _caImage, _caName, _caSubID, _caID;
		#endregion


		public Tbl002_Category()
		{
			// Nothing for now.
		}


		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl002_Category_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caName));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaSubID", SqlDbType.NChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caSubID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaType", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caType));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caStatus));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaImage", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caImage));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaLink", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caLink));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl002_Category::Insert::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl002_Category_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caName));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaSubID", SqlDbType.NChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caSubID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaType", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caType));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caStatus));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaImage", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caImage));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaLink", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caLink));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl002_Category::Update::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl002_Category_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _caID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl002_Category::Delete::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl002_Category_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl002_Category");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _caID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_caID = (string)toReturn.Rows[0]["CaID"];
					_caName = toReturn.Rows[0]["CaName"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CaName"];
					_caSubID = toReturn.Rows[0]["CaSubID"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CaSubID"];
					_caType = toReturn.Rows[0]["CaType"] == System.DBNull.Value ? SqlBoolean.Null : (bool)toReturn.Rows[0]["CaType"];
					_caStatus = toReturn.Rows[0]["CaStatus"] == System.DBNull.Value ? SqlBoolean.Null : (bool)toReturn.Rows[0]["CaStatus"];
					_caImage = toReturn.Rows[0]["CaImage"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CaImage"];
					_caLink = toReturn.Rows[0]["CaLink"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CaLink"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl002_Category::SelectOne::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl002_Category_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl002_Category");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl002_Category::SelectAll::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlString CaID
		{
			get
			{
				return _caID;
			}
			set
			{
				SqlString caIDTmp = (SqlString)value;
				if(caIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("CaID", "CaID can't be NULL");
				}
				_caID = value;
			}
		}


		public SqlString CaName
		{
			get
			{
				return _caName;
			}
			set
			{
				_caName = value;
			}
		}


		public SqlString CaSubID
		{
			get
			{
				return _caSubID;
			}
			set
			{
				_caSubID = value;
			}
		}


		public SqlBoolean CaType
		{
			get
			{
				return _caType;
			}
			set
			{
				_caType = value;
			}
		}


		public SqlBoolean CaStatus
		{
			get
			{
				return _caStatus;
			}
			set
			{
				_caStatus = value;
			}
		}


		public SqlString CaImage
		{
			get
			{
				return _caImage;
			}
			set
			{
				_caImage = value;
			}
		}


		public SqlString CaLink
		{
			get
			{
				return _caLink;
			}
			set
			{
				_caLink = value;
			}
		}
		#endregion
	}
}
